package com.cmd.driver;


import com.alibaba.fastjson.JSONObject;
import com.player.DriverUser;
import com.player.User;
import com.qy.bean.DriverInfo;
import com.qy.util.IDCardUtil;
import com.qy.util.RedisPoolMgr;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 
 * 司机注册
 * @author zwl
 * 2018年2月1日
 */
public class DriverRegisterCMD  extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
	    	JSONObject reJs = new JSONObject();
	    	reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		JSONObject param = packet.getMessage();
		if(!param.containsKey("phone")||!param.containsKey("vCode")||
			!param.containsKey("userName")||!param.containsKey("card")||
			!param.containsKey("drivingLicenceImage1")||!param.containsKey("drivingLicenceImage2")||
			!param.containsKey("headImage")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			user.sendPacket(reJs);
			return;
		}
		//身份证号码
		String card = param.getString("card");
		if(!IDCardUtil.isIDCard(card)){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.CARD_ERROR.getType());
			user.sendPacket(reJs);
			return;
		}
		String phone = param.getString("phone");
		//用户输入的验证码
		String vCode = param.getString("vCode");
		//服务器保存的验证码
		String mCode = (String) RedisPoolMgr.get(user.getKey()+phone, String.class);
		/*
		 * 判断验证码是否正确
		 * 不正确则错误次数+1
		 * 正确则错误次数清零
		 */
		Object tmp =  RedisPoolMgr.hashGet(StaticData.REDISKEY.V_CODE_ERROR.toString(), user.getKey());
		if(!vCode.equals(mCode)){
			int count = 0;
			if(tmp!=null&&!tmp.equals("null")){
				count = ((int) tmp)+1;
			}
			RedisPoolMgr.hashSet(StaticData.REDISKEY.V_CODE_ERROR.toString(), user.getKey(), count,10*60);
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.verify_error.getType());
			user.sendPacket(reJs);
			return;
		}
		//正确则错误次数清零	
		if(tmp!=null&&!tmp.equals("null")){
		    RedisPoolMgr.hashDel(StaticData.REDISKEY.V_CODE_ERROR.toString(), user.getKey());
		}
		//用戶姓名
		String userName = param.getString("userName");

		//驾驶证正反面
		String drivingLicenceImage1 = param.getString("drivingLicenceImage1");
		String drivingLicenceImage2 = param.getString("drivingLicenceImage2");
		
		int sex = 0;
		String headImage = param.getString("headImage");
		DriverUser driverUser = (DriverUser) user;
		DriverInfo driverInfo = driverUser.getDriverInfo();
		driverInfo.setUserName(userName);
		driverInfo.setCard(card);
		driverInfo.setDrivingLicenceImage1(drivingLicenceImage1);
		driverInfo.setDrivingLicenceImage2(drivingLicenceImage2);
		driverInfo.setSex(sex);
		driverInfo.setImage(headImage);
		driverInfo.setPhone(phone);
		driverInfo.setUserStatus(StaticData.driverStatus.wait_check.getType());
		
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		user.sendPacket(reJs);
		return;
	}

}
